<template>
  <view class="login-container">
    <view class="login-header">
      <text class="login-title">智慧农业助手</text>
      <text class="login-subtitle">走进智慧农业，创建幸福生活</text>
    </view>
    
    <view class="login-box">
      <view class="login-tabs">
        <view 
          class="tab-item" 
          :class="{ active: activeTab === 'account' }" 
          @click="activeTab = 'account'"
        >
          账号密码登录
        </view>
        <view 
          class="tab-item" 
          :class="{ active: activeTab === 'sms' }" 
          @click="activeTab = 'sms'"
        >
          手机验证码登录
        </view>
        <view class="tab-line" :style="tabLineStyle"></view>
      </view>
      
      <view class="login-form">
        <view class="input-item">
          <view class="input-icon">
            <uni-icons type="person" size="20" color="#bbb"></uni-icons>
          </view>
          <input 
            v-model="formData.username" 
            type="text" 
            placeholder="请输入用户名/手机号" 
            class="form-input"
          />
        </view>
        
        <view class="input-item" v-if="activeTab === 'account'">
          <view class="input-icon">
            <uni-icons type="locked" size="20" color="#bbb"></uni-icons>
          </view>
          <input 
            v-model="formData.password" 
            type="password" 
            placeholder="请输入密码" 
            class="form-input"
          />
        </view>
        
        <view class="input-item" v-if="activeTab === 'sms'">
          <view class="input-icon">
            <uni-icons type="locked" size="20" color="#bbb"></uni-icons>
          </view>
          <input 
            v-model="formData.smsCode" 
            type="text" 
            placeholder="请输入验证码" 
            class="form-input"
          />
          <button class="sms-button">获取验证码</button>
        </view>
        
        <button class="login-button" @click="handleLogin">登 录</button>
      </view>
    </view>
    
    <view class="third-party-login">
      <text class="third-party-title">使用第三方账号登录</text>
      <view class="third-party-icons">
        <view class="icon-item"style="background-color:#298be1;">

          <uni-icons type="qq" size="30" color="white"></uni-icons>
        </view>
        <view class="icon-item"style="background-color:#29e145;">
          <uni-icons type="weixin" size="30" color="white"></uni-icons>
        </view>
        <view class="icon-item"style="background-color:#e16907;">
          <uni-icons type="weibo" size="30" color="white"></uni-icons>
        </view>
      </view>
      <view class="register-link">
        <text>没有账号？</text>
        <text class="link">手机号快捷登录</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
// 引入 uni-app API
import * as uni from '@dcloudio/uni-app';

// 表单数据
const formData = ref({
  username: '',
  password: '',
  smsCode: ''
});

// 当前激活的标签
const activeTab = ref('account');

// 计算标签线的样式
const tabLineStyle = computed(() => {
  return {
    transform: activeTab.value === 'account' ? 'translateX(0)' : 'translateX(100%)'
  };
});

// 登录处理函数
const handleLogin = () => {
  if (activeTab.value === 'account') {
    if (!formData.value.username || !formData.value.password) {
      uni.showToast({
        title: '请输入用户名和密码',
        icon: 'none'
      });
      return;
    }
    // 账号密码登录逻辑
    console.log('账号密码登录', formData.value);
  } else {
    if (!formData.value.username || !formData.value.smsCode) {
      uni.showToast({
        title: '请输入手机号和验证码',
        icon: 'none'
      });
      return;
    }
    // 验证码登录逻辑
    console.log('验证码登录', formData.value);
  }
  
  // 模拟登录请求
  uni.showLoading({
    title: '登录中...'
  });
  
  setTimeout(() => {
    uni.hideLoading();
    uni.showToast({
      title: '登录成功',
      icon: 'success'
    });
    // 登录成功后的跳转
    // uni.switchTab({
    //   url: '/pages/index/index'
    // });
  }, 1500);
};
</script>

<style>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  background-color: #e6f3f9;
  padding: 40rpx;
}

.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 80rpx;
  margin-bottom: 60rpx;
}

.login-title {
  font-size: 48rpx;
  font-weight: bold;
  color: #4cbeb3;
  margin-bottom: 20rpx;
}

.login-subtitle {
  font-size: 28rpx;
  color: #666;
}

.login-box {
  width: 100%;
  background-color: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
}

.login-tabs {
  display: flex;
  position: relative;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 40rpx;
}

.tab-item {
  flex: 1;
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
  position: relative;
}

.tab-item.active {
  color: #4cbeb3;
  font-weight: bold;
}

.tab-line {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 50%;
  height: 4rpx;
  background-color: #4cbeb3;
  transition: transform 0.3s;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 30rpx;
}

.input-item {
  display: flex;
  align-items: center;
  height: 90rpx;
  border: 1px solid #eee;
  border-radius: 8rpx;
  padding: 0 20rpx;
  background-color: #fff;
}

.input-icon {
  margin-right: 20rpx;
  display: flex;
  align-items: center;
}

.form-input {
  flex: 1;
  height: 100%;
  border: none;
  font-size: 28rpx;
  color: #333;
}

.sms-input {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.sms-button {
  width: 200rpx;
  height: 80rpx;
  line-height: 80rpx;
  font-size: 24rpx;
  background-color: #f0f0f0;
  color: #666;
  border: none;
  border-radius: 8rpx;
}

.login-button {
  height: 90rpx;
  line-height: 90rpx;
  background-color: #4cbeb3;
  color: #fff;
  font-size: 32rpx;
  border: none;
  border-radius: 45rpx;
  margin-top: 40rpx;
}

.third-party-login {
  margin-top: 80rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.third-party-title {
  font-size: 26rpx;
  color: #999;
  margin-bottom: 40rpx;
}

.third-party-icons {
  display: flex;
  justify-content: center;
  gap: 60rpx;
  margin-bottom: 60rpx;
}

.icon-item {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.qq {
  background-color: #4cafe9;
}

.wechat {
  background-color: #50b674;
}

.weibo {
  background-color: #eb5e5f;
}

.register-link {
  font-size: 26rpx;
  color: #666;
}

.link {
  color: #4cbeb3;
}
</style>